// pages/identity/identity.js
Page({

  data: {
    /*姓名*/
    name:'',
    /*性别*/
    g1:false,
    g2:false,
    gender:'',
    /*电话 */
    number:'',
    /*身份 */
    identity1:false,
    identity2:false,
    identity:'',
    stunumber:'',
    academy:'',
    talent:'',
    class:''

  },

// 加载用户信息
  onShow(){
    var that = this;
    var isuser = wx.getStorageSync('isuser');
    if(isuser != "no"){
      var openid = wx.getStorageSync('openid');
      wx.request({
        url: 'http://127.0.0.1:8080/user/personInfo',
        method:"POST",
        data:{openid:openid},
        success(res){
          // console.log(res)
          var personinfo = res.data[0];
          // console.log(personinfo)
          that.setData({
            name:personinfo.userName,
            number:personinfo.phone
          })
          //性别信息
          if(personinfo.userGender == "男"){
            that.setData({
              g1:true,
              gender:"男"
            })
          }else{
            that.setData({
              g2:true,
              gender:"女"
            })
          }
          //用户身份信息
          if(personinfo.iden == "学生用户"){
            that.setData({
              identity1:true,
              stunumber:personinfo.stuNumber,
              academy:personinfo.academy,
              talent:personinfo.profession,
              class:personinfo.userClass,
              identity:"学生用户"
            })
          }else{
            that.setData({
              identity2:true,
              identity:"房屋出租方"
          })
          }
        }
      })
    }
   

  },

// 获取姓名
  getname(e){
    this.setData({
      name:e.detail.value
    })
    // console.log(this.data.name)
  }

//修改性别
  ,choosegende1(){
    this.setData({
      g1:true,
      g2:false,
      gender:"男"
    })
  },choosegende2(e){
    console.log(e)
    this.setData({
      g2:true,
      g1:false,
      gender:"女"
    })
  },

/*获取电话 */
  getnumber(e){
    this.setData({
      number:e.detail.value
    })
  },

//选择身份
  chooseidentity1(e){
    this.setData({
      identity1:true,
      identity2:false,
      identity:"学生用户"
    })
  },
  chooseidentity2(e){
    this.setData({
      identity1:false,
      identity2:true,
      identity:"房屋出租方"
    })
  },

// 获取学号，学院，专业，班级
  stunumber(e){
      this.setData({
        stunumber:e.detail.value
      })
  },
  academy(e){
    this.setData({
      academy:e.detail.value
    })
  },
  talent(e){
    this.setData({
      talent:e.detail.value
    })
  },
  class(e){
    this.setData({
      class:e.detail.value
    })
  },

  // 提交以及修改信息
  submit(){

    
    var that = this;
    var personinfo = this.data;
    var isuser = wx.getStorageSync('isuser');
    var openid = wx.getStorageSync('openid')

    if(personinfo.name == "" || personinfo.gender == "" || personinfo.number == "" || personinfo.identity == ""){
      wx.showToast({
        title: '信息不完整',
        duration: 2000,
        icon: "error",
        success: (res) => {},
      })
    }else{
      if(personinfo.identity == "学生用户"){
        if(personinfo.stunumber == "" || personinfo.academy == "" || personinfo.talent == "" || personinfo.class == ""){
          wx.showToast({
            title: '学生信息不全',
            duration: 2000,
            icon: "error",
            success: (res) => {},
          })
        }else{
          if(isuser == "no"){
            // console.log(wx.getStorageSync('userinfo').nickname)
            //插入用户
            wx.request({
              url: 'http://127.0.0.1:8080/user/addUser',
              method:"POST",
              data:{
                openid:openid,
                nickname:wx.getStorageSync('userinfo').nickName,
                ava:wx.getStorageSync('userinfo').avatarurl,
                name:personinfo.name,
                gender:personinfo.gender,
                number:personinfo.number,
                stuNumber:personinfo.stunumber,
                academy:personinfo.academy,
                profession:personinfo.talent,
                userClass:personinfo.class,
                iden:personinfo.identity,
            },success(res){
              wx.showToast({
                title: '提交成功',
                icon:"success"
              })
              console.log(res)
              that.onLoad();
            }
            })
          }else{
            // 更新用户
            console.log(this.data)
            wx.request({
            url: 'http://127.0.0.1:8080/user/updateInfo',
            method:"POST",
            data:{
                openid:openid,
                name:personinfo.name,
                gender:personinfo.gender,
                number:personinfo.number,
                stuNumber:personinfo.stunumber,
                academy:personinfo.academy,
                profession:personinfo.talent,
                userClass:personinfo.class,
                iden:personinfo.identity,
            },
            success(res){
              wx.showToast({
                title: '提交成功',
                icon:"success"
              })
                console.log(res)
                that.onLoad();
            }
            })
          }
         
        }
      }else{
        if(isuser == "no"){
          //插入房屋出租用户
          wx.request({
            url: 'http://127.0.0.1:8080/user/addUser',
            method:"POST",
            data:{
              openid:openid,
              name:personinfo.name,
              gender:personinfo.gender,
              number:personinfo.number,
              stuNumber:null,
              academy:null,
              profession:null,
              userClass:null,
              iden:personinfo.identity,
          },success(res){
            wx.showToast({
              title: '提交成功',
              icon:"success"
            })
            console.log(res)
            that.onLoad();
          }
          })
        }else{
          // 更新房屋出租用户
          wx.request({
            url: 'http://127.0.0.1:8080/user/updateInfo',
            method:"POST",
            data:{
                openid:openid,
                name:personinfo.name,
                gender:personinfo.gender,
                number:personinfo.number,
                stuNumber:null,
                academy:null,
                profession:null,
                userClass:null,
                iden:personinfo.identity,
            },
            success(res){
              wx.showToast({
                title: '提交成功',
                icon:"success"
              })
                console.log(res)
                that.setData({
                  stunumber:null,
                  academy:null,
                  class:null,
                  talent:null
                })
            }
            })
        }
      }
    }
    
    

  }

})